<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="理财申购编号" prop="financePurchaseNumber" label-width="100px">
        <el-input v-model="queryParams.financePurchaseNumber" placeholder="请输入理财申购编号" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="投资业务名称" prop="financePurchaseName" label-width="100px">
        <el-input v-model="queryParams.financePurchaseName" placeholder="请输入投资业务名称" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="理财类型" prop="financePurchaseNumber" label-width="100px">
        <el-input v-model="queryParams.financePurchaseNumber" placeholder="请输入理财申购编号" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="申购审批状态" prop="purchaseStatus" label-width="100px">
        <el-select v-model="queryParams.purchaseStatus" placeholder="请选择申购审批状态" clearable>
          <el-option v-for="dict in dict.type.sys_approve_status" :key="dict.value" :label="dict.label"
                     :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini"
                   @click="handleAdd" v-hasPermi="['loan:financePurchase:add']">新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single"
                   @click="handleUpdate" v-hasPermi="['loan:financePurchase:edit']">修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
                   @click="handleDelete" v-hasPermi="['loan:financePurchase:remove']">删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini"
                   @click="handleExport" v-hasPermi="['loan:financePurchase:export']">导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="financePurchaseList" @selection-change="handleSelectionChange" border
              height="calc(100vh - 300px)">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="理财申购编号" align="center" prop="financePurchaseNumber" min-width="150px">
        <template slot-scope="scope">
          <el-button type="text" @click="openPurchaseDetails(scope.row.financePurchaseId)" v-text="scope.row.financePurchaseNumber"></el-button>
        </template>
      </el-table-column>
      <el-table-column label="投资业务名称" align="center" prop="financePurchaseName" min-width="150px" :show-overflow-tooltip="true"/>
      <el-table-column label="理财产品名称" align="center" prop="financeProductsIdN" min-width="150px"/>
      <el-table-column label="自动赎回" align="center" prop="" min-width="150px"/>
      <el-table-column label="赎回规则(T+n)" align="center" prop="" min-width="150px"/>
      <el-table-column label="固定期限(自然日)" align="center" prop="" min-width="150px"/>
      <el-table-column label="合作机构" align="center" prop="" min-width="150px"/>
      <el-table-column label="理财类型" align="center" prop="" min-width="150px"/>
      <el-table-column label="申购金额" align="center" prop="purchaseAmount" min-width="150px"/>
      <el-table-column label="申购剩余可赎回金额" align="center" prop="leftRedemptionAmount" min-width="150px"/>
      <el-table-column label="申购日期" align="center" prop="actualPurchaseDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.actualPurchaseDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预计赎回到期日" align="center" prop="expectedDueDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expectedDueDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="申购审批状态" align="center" prop="purchaseStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_approve_status" :value="scope.row.purchaseStatus"/>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!--  明细 抽屉  -->
    <el-drawer title="理财申购明细" :visible.sync="openDetail" direction="rtl" size="95%">
      <PurchaseDetails :select="openDetailParam"></PurchaseDetails>
    </el-drawer>
  </div>
</template>

<script>
import {
  listFinancePurchase,
  getFinancePurchase,
  delFinancePurchase,
  addFinancePurchase,
  updateFinancePurchase
} from "@/api/loan/financePurchase";

import PurchaseDetails from "./detail";

export default {
  name: "FinancePurchase",
  dicts:['sys_approve_status'],
  components:{PurchaseDetails},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 申购/赎回管理表格数据
      financePurchaseList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        financeProductsId: null,
        financePurchaseNumber: null,
        financePurchaseName: null,
        planInvestmentAmount: null,
        purchaseAmount: null,
        redemptionAmount: null,
        planPurchaseDate: null,
        actualPurchaseDate: null,
        expectedDueDate: null,
        expectedDueDateChange: null,
        expectedDueDateChangeFlag: null,
        actualInvestmentAmount: null,
        description: null,
        rollOutAccountId: null,
        rollOutAmount: null,
        rollInAmount: null,
        purchaseStatus: null,
        redemptionStatus: null,
        debitAccount: null,
        documentType: null,
        documentCategory: null,
        businessType: null,
        monthCollectionFlag: null,
        leftRedemptionAmount: null,
        annualYield: null,
      },
      //打开详细页面传入的参数
      openDetailParam:{},
      // 是否显示弹出层
      openDetail: false
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询申购/赎回管理列表 */
    getList() {
      this.loading = true;
      listFinancePurchase(this.queryParams).then(response => {
        this.financePurchaseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.financePurchaseId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.openPurchaseDetails(null,'modify');
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const financePurchaseId = row.financePurchaseId || this.ids
      getFinancePurchase(financePurchaseId).then(response => {
        this.openPurchaseDetails(response.data.financePurchaseId,'modify');
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const financePurchaseIds = row.financePurchaseId || this.ids;
      this.$modal.confirm('是否确认删除申购/赎回管理编号为"' + financePurchaseIds + '"的数据项？').then(function () {
        return delFinancePurchase(financePurchaseIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('loan/financePurchase/export', {
        ...this.queryParams
      }, `financePurchase_${new Date().getTime()}.xlsx`)
    },
    /**打开详细页面*/
    openPurchaseDetails(financePurchaseId,funcUsage){
      this.openDetailParam = {
        financePurchaseId:financePurchaseId,
        funcUsage:funcUsage||'modify',
        //关闭页面的回调
        cancel: () => {
          this.openDetail = false;
        },
      };
      this.openDetail = true;
    }
  }
};
</script>
